#include "game.h"

//文件列表
Obj*list;
//记录初始按下位置
Obj*touch;


Spr*file_spr;
Spr*folder_spr;
Obj*back_obj;

//绘制路径obj，路径data[0]
//data[1]宽度，data[2]高度
void drawPath(void*ob)
{
 Obj*obj = ob;
 //获取
 int w = maxInt(obj->data[1],100);
 int h = maxInt(obj->data[2],40);
 char*path = (char*)obj->data[0];
 clipRect(obj->x,obj->y,w,h);
 
 //绘制背景
 drawRect(obj->x,obj->y,
  SW-obj->x,h,0x80ffffff);
 drawRect(obj->x+1,obj->y+1,
  SW-obj->x-2,h-2,0xff65aef0);
 if(obj->data[0]!=-1)
 {
  //截取路径
  int textw = 
  fnt_getLineW(GM.fnt,path);
  fnt_drawline(GM.fnt,path,
   obj->x-(textw-w)-5,obj->y+
   (h-fnt_getLineW(GM.fnt,"A"))/2);
 }
 clipRect(0,0,SW,SH);
}

//绘制文件列表，路径data[0]
//data[1]总宽度，data[2]单元高度
//data[3]总高度，data[4]y偏移
//data[5]总项目，data[6]选中项目
//data[7]选中name
void drawFileList(void*ob)
{
 Obj*obj = ob;
 if(obj->data[0]==-1)
 	return;
 
 //路径
 char name[100];
 char data[500];
 int w = maxInt(obj->data[1],100);
 int h = maxInt(obj->data[2],40);
 int mh = maxInt(obj->data[3],40);
 int dy = obj->data[4];
 int se = obj->data[6];
 char*path = (char*)obj->data[0];
 
 //绘制标题
 drawPath(obj);
 clipRect(obj->x,obj->y+h,w,mh);
 
 //遍历文件列表
 int32 find = 
 findstart(path,name,100);
 int isEnd = find;
 int count = 1;
 while(isEnd!=-1)
 {
  if(strcmp(name,"..")!=0)
  	if(strcmp(name,".")!=0)
  {
   if(obj->y+dy+count*h>0)
   if(obj->y+dy+count*h<SH)
   {
   //绘制背景
   drawRect(obj->x,obj->y+dy+
    count*h,
    SW-obj->x,h,0xfff0f0f0);
    
   //选中
   if(se!=count)
   	drawRect(obj->x+1,obj->y+dy+1+
    count*h,
    SW-obj->x-2,h-2,0xffffffff);
   else
   {
    obj->data[7] = 
    getRes(name,tStr)->id;
    drawRect(obj->x+1,obj->y+dy+1+
     count*h,
     SW-obj->x-2,h-2,0xfff0f0f0);
   }
   
   memset(data,'\0',500);
   sprintf(data,"%s/%s",path,name);
   //printf("%s\n",data);
   //绘制文件/文件夹
   if(filestate(data)==IS_FILE)
   {
    drawSpr(file_spr,obj->x,
     obj->y+dy+5+count*h);
   }
   else
   {
    drawSpr(folder_spr,obj->x,
     obj->y+dy+5+count*h);
   }
   
   //绘制
   fnt_drawline(GM.fnt,
    name,obj->x+10+h,
    obj->y+dy+10+count*h);
   //绘制路径
   fnt_drawline(GM.fnt_small,
    name,obj->x+10+h,
    obj->y+dy+10+count*h+50);
   }
   count++;
  }
  isEnd = findnext(find,name,100);
 }
 obj->data[5] = count;
 findstop(find);
 clipRect(0,0,SW,SH);
}

//打开文件
void openFile(char*path)
{
 char data[300];
 char*code = "{\"type\":\"*/*\",\"data\": \"sdcard/%s\",\"action\":\"android.intent.action.VIEW\"}";
 
 sprintf(data,code,path);
 char*retext = 
 ex_call("startActivity",data);
 if(retext!=NULL)
 {
  printf(data);
  printf(retext);
  free(retext);
 }
}

//刷新列表
//data[1]总宽度，data[2]单元高度
//data[3]总高度，data[4]y偏移
//data[5]总项目，data[6]选中项目
//data[7]选中name
void refList(Obj*obj,char*path)
{
 if(path!=NULL)
 {
 if(filestate(path)==IS_DIR)
 {
 	obj->data[0] = (int)path;
  obj->data[4] = 0;
 obj->data[6] = 0;
 }
 else 
  openFile(path);
 }
}



//房间创建
void homeCreate(void*rm)
{
 Obj*obj;
 Spr*spr;
 Room*room = rm;
 //载入资源
 GM.fnt = (_FNT*)
 getRes("fnt/font.fnt",tFnt)->id;
 GM.fnt_small = (_FNT*)
 getRes("fnt/small.fnt",tFnt)->id;
 spr = (Spr*)
 getRes("spr/file.spr",tSpr)->id;
 file_spr=spr;
 spr = (Spr*)
 getRes("spr/folder.spr",tSpr)->id;
 folder_spr=spr;
 
 spr = (Spr*)
 getRes("spr/back.spr",tSpr)->id;
 obj = newObj(SW-120,SH-80,spr);
 obj->data[0]=5;
 back_obj=obj;
 roomAddObj(room,obj);
 
 //文件列表
 obj = newObj(0,0,NULL);
 obj->data[1] = SW;
 obj->data[2] = 100;
 obj->data[3] = SH-200;
 obj->data[4] = 0;
 obj->data[5] = 0;
 obj->draw = drawFileList;
 list = obj;
 roomAddObj(room,obj);
 
 obj = newObj(0,0,NULL);
 touch = obj;
 roomAddObj(room,obj);
 refList(list,"capp");
}

//绘制房间
void homeDraw(void*rm)
{
 Room*room = rm;
 drawRect(0,SH-100,
 SW,100,0xfff0f0f0);
 drawSenObjCam(room->sen,
  &(room->cam));
}

//房间事件
void homeEvent(int type,int p1,int p2)
{
 int dd,dy;
 char*temp;
 char data[500];
 if(MS_DOWN==type)
 shake(10);
 
 //判断是否点击返回
 if(MS_DOWN==type)
 {
  dd=_posCollObj(p1,p2,back_obj);
  if(dd)
  {
    homeEvent(KY_UP,_BACK,0);
    return; 
  }
 }
 
 //按下
 if(MS_DOWN==type)
 {
  touch->y = p2;
  touch->y = p2;
  //计算选中
  dy = p2-list->y-list->data[4];
  dd = dy/list->data[2];
  dd=maxInt(dd,1);
  if(list->data[6]==dd)
  {
   temp=(char*)list->data[0];
   if(strcmp(temp,"")==0)
   sprintf(data,"%s",list->data[7]);
   else
   sprintf(data,"%s/%s",
    temp,list->data[7]);
   temp = (char*)
   getRes(data,tStr)->id;
   refList(list,temp);
  }
  else if(dd<list->data[5])
  {
  	list->data[6] = dd;
  }
  else
  list->data[6] = -1;
 }
 
 //归位
 if(MS_UP==type)
 {
  list->data[4]
   = minInt(list->data[4],0);
 }
 
 //拖拽
 if(MS_MOVE==type)
 {
  list->data[4]-=touch->y-p2;
  touch->y = p2;
 }
 
 //退出页面
 if(KY_UP==type)
 {
  switch(p1)
  {
   case _BACK:
   temp=(char*)list->data[0];
   if(temp==NULL
   ||strcmp(temp,"")==0
   ||strcmp(temp,"/")==0)
   {
    exitRoom(_room);
    exit();
   }
   else
   {
    char*pos=strrchr(temp, '/');
    if(pos==NULL)
     refList(list,"");
    else
    {
     memset(data,'\0',500);
     dd=strlen(temp)-strlen(pos);
     strncpy(data, temp, dd);
     refList(list,(char*)
     getRes(data,tStr)->id);
    }
   }
   break;
   case _MENU:
   break;
  }
 }
}

//跳转房间
int gotoHome()
{
 if(GM.rmHome==NULL)
 {
  GM.rmHome = 
  newRoom(SW,SH,100);
 }
 GM.rmHome->create = homeCreate;
 GM.rmHome->draw = homeDraw;
 GM.rmHome->event = homeEvent;
 gotoRoom(GM.rmHome);
 return 0;
}




